﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 交接班 : Form
    {
        public 交接班()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 登陆模块
        /// </summary>
        /// <param name="lpstrID">账号</param>
        /// <param name="lpstrPW">密码</param>
        private void login(string lpstrID, string lpstrPW)
        {
            int req = LOGIN_ERROR_UNKNOWN;
            DataSet ds;
            try
            {
                ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, "select * from 员工账号", null);//.Tables[0].DefaultView;
            }
            catch (Exception e)
            {
#if DEBUG
                Console.Write(e.Message);
#endif
                MessageBox.Show("系统发生未知错误，错误信息:" + e.Message);
                return;
            }
            MySqlHelper.ManagerInfo[] m = MySqlHelper.GetInfo(ds.Tables[0]);
            MySqlHelper.ManagerInfo info = new MySqlHelper.ManagerInfo();
            for (int i = 0; i < m.Length; i++)
            {
                // 匹配账号
                if (lpstrID.Equals(m[i].user))
                {
                    //匹配密码
                    if (lpstrPW.Equals(m[i].password))
                    {
                        //全部匹配成功
                        //m_lpstrUserName
                        主窗口.m_MangerInfo = m[i];
                        req = LOGIN_SUSSESS;
                        info = m[i];
                        break;
                    }
                }

                if (i == m.Length - 1)
                {
                    // 到最后一个还没有匹配出合适的账户
                    req = LOGIN_ERROR_ID;
                }

            }


            switch (req)
            {
                case LOGIN_SUSSESS:
                    // 登陆成功
                    //MessageBox.Show(STR_LOGIN_SUSSESS);
                    客服窗口.create(主窗口.m_MangerInfo,null);
                    Log.managerLog("登陆客户端", info.id, info.name, info.user);
                    主窗口.startTime = DateTime.Now;
                    this.Hide();
                    break;
                case LOGIN_ERROR_PASSWORD:
                    MessageBox.Show(STR_LOGIN_ERROR_PASSWORD);
                    break;
                case LOGIN_ERROR_ID:
                    MessageBox.Show(STR_LOGIN_ERROR_ID);
                    break;
                case LOGIN_ERROR_NETWORK:
                    MessageBox.Show(STR_LOGIN_ERROR_NETWORK);
                    break;
                case LOGIN_ERROR_UNKNOWN:
                    MessageBox.Show(STR_LOGIN_ERROR_UNKNOWN);
                    break;
            }

        }
        /// <summary>
        /// 登陆成功
        /// </summary>
        public const int LOGIN_SUSSESS = 0;
        /// <summary>
        /// 密码错误
        /// </summary>
        public const int LOGIN_ERROR_PASSWORD = 1;
        /// <summary>
        /// 账号错误
        /// </summary>
        public const int LOGIN_ERROR_ID = 2;
        /// <summary>
        /// 网络错误
        /// </summary>
        public const int LOGIN_ERROR_NETWORK = 3;
        /// <summary>
        /// 未知错误
        /// </summary>
        public const int LOGIN_ERROR_UNKNOWN = 4;

        public const string STR_LOGIN_SUSSESS = "登陆成功";
        public const string STR_LOGIN_ERROR_PASSWORD = "密码错误";
        public const string STR_LOGIN_ERROR_ID = "您的账号或密码错误，请联系管理员";
        public const string STR_LOGIN_ERROR_NETWORK = "网络错误";
        public const string STR_LOGIN_ERROR_UNKNOWN = "未知错误";
        

        private void button添加_Click(object sender, EventArgs e)
        {
            //构造并传送交接班信息
            login(textBox账号.Text, textBox密码.Text);
            //DataBase.getInstance().managerExit(s);
        }

        private void 交接班_Load(object sender, EventArgs e)
        {
            DataBase.ManagerExitState s = new DataBase.ManagerExitState();
            s.stratTime = 主窗口.startTime;
            s.endTime = DateTime.Now;
            s.id = 客服窗口.m_ManagerInfo.user;
            s.name = 客服窗口.m_ManagerInfo.name;
            //获取一段时间内所有钱数之和
            double money;
            DataBase.getInstance().getMoneySumForTime(主窗口.startTime, s.endTime, 客服窗口.m_ManagerInfo.id, out money);
            s.money = money;
            textBox金额.Text = money.ToString();
        }
    }
}
